Skip to content

perf: Evaluate the branches of oneOf, anyOf, not and if up to their first error - #1007

Open
Roman3349 wants to merge 2 commits into
jsonrainbow:mainfrom
Roman3349:perf/fail-fast-branches
Open

Roman3349 wants to merge 2 commits into
jsonrainbow:mainfrom
Roman3349:perf/fail-fast-branches

Conversation

@Roman3349

Copy link
Copy Markdown
Contributor

Description

In strict mode, every branch of oneOf and anyOf and the subschemas of not and if were evaluated completely, although only their validity is used: the errors of the branches are discarded when the oneOf or anyOf passes, and the errors of not and if are never reported. With schemas that select between several large alternatives, this is where most of the time goes. In the GeoJSON schema of the jsonschema-benchmark, the root oneOf has 9 branches and geometry another 7, so the coordinates of each geometry are walked through in about 6 branches, of which only one matches.

The branches are now evaluated up to their first error. When the oneOf or anyOf fails, its branches are evaluated once more to collect their errors, so the reported errors are the same as before. This is skipped as well when the errors are not needed either, for example in a branch of an outer oneOf.

Collecting all errors stays the default. The new Constraint::CHECK_MODE_FAIL_FAST stops the whole validation at its first error and reports only that one, for when only the validity of the data is needed.

Changes:

  • Constraints\EvaluationState: new, holds whether the constraints may stop at their first error. It is shared by all factories of one evaluation, including the ones the strict mode draft constraints create for nested schemas.
  • Constraints\Factory: startFailFast(), stopFailFast() and isFailFast() to switch it for the evaluation of a subschema. It stays off when the check mode modifies the value (CHECK_MODE_APPLY_DEFAULTS, CHECK_MODE_COERCE_TYPES) or throws on the first error (CHECK_MODE_EXCEPTIONS), as every constraint has to be evaluated then.
  • Draft-06, Draft-07 and Draft 2019-09:
    • OneOfConstraint, AnyOfConstraint, NotConstraint and IfThenElseConstraint (for if) evaluate their subschemas up to the first error.
    • The draft constraints skip the remaining keywords, and ItemsConstraint and PropertiesConstraint the remaining items and properties, once an error is found and only the validity is needed.
  • Constraint::CHECK_MODE_FAIL_FAST (0x00000400) and Validator: the new check mode, documented in the README.

The perf and feat parts are in separate commits.

Validation of the 500 GeoJSON instances with the benchmark's harness for this library (implementations/justinrainbow/main.php, strict mode):

Cold Warm
main 286.9 s 287.0 s
This PR 144.6 s 145.1 s

Related Issue

None

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Code refactoring
  • Other (please describe):

Checklist

  • I have read the CONTRIBUTING guidelines
  • My code follows the code style of this project
  • I have added tests that prove my fix is effective or that my feature works
  • All new and existing tests pass
  • I have updated the documentation accordingly
  • My changes generate no new warnings

Additional Notes

FailFastTest checks that the errors reported by default are exactly the ones of main for failing oneOf, anyOf, not, if and nested oneOf in all three drafts, and that CHECK_MODE_FAIL_FAST reports a single error.

This works together with #926, which avoids instantiating the constraints of keywords a schema does not have. With both, the first 100 GeoJSON instances take 5.6 s instead of 26.7 s on main (11.0 s with #926 alone, 11.4 s with this PR alone). Both change checkForKeyword() of the draft constraints, so whichever is merged second needs a small rebase.

#971 (Draft 2020-12) uses the next bit, 0x00000800, for CHECK_MODE_FORMAT_ASSERTION.

…irst error

Signed-off-by: Roman Ondráček <mail@romanondracek.cz>
…the first error

Signed-off-by: Roman Ondráček <mail@romanondracek.cz>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant